package rdf_Encoder;


import java.io.File;

import gps_Convertor.GPSConvertor;

import com.google.common.collect.HashBiMap;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.FileManager;



public class Store {
	
	private static HashBiMap<String, Integer> lookupTable = HashBiMap.create();
	private static boolean DEBUG = false;

	static final String RDFfileName [] = {"geo-states.n3" };
	
	/********************************************************************************
	 * This method is used to read the RDF file and create the 
	 * corresponding encoded LookUp Table
	 ********************************************************************************/
	public void readRDF() {
		int SOIdent = 0;    //Subject and Object identifiers 
		int predIdent = -1; //Predicate identifier 
		
		int tripleCnt = 0;
		for (int j = 0 ; j < RDFfileName.length ; j++){
			
			//Loading the file to the model object
			Model m = FileManager.get().loadModel(RDFfileName[j]);
			//Iterator for the file
			StmtIterator iter = m.listStatements(); 

			while (iter.hasNext()) {
				
				/***********************************************
				 * Creating Lookup Table
				 ***********************************************/
				//Creating Subject, predicate and object object. 
				Statement stmt      = iter.nextStatement();  
				Resource  subject   = stmt.getSubject();     
				Property  predicate = stmt.getPredicate();   
				RDFNode   object    = stmt.getObject(); 

				String sub = subject.toString();
				String prd = predicate.toString();
				String obj = object.toString();
				
				int subId = 0;
				int prdId = 0;
				int objId = 0;
				
				//Create look-up entry for Subject. 
				if (lookupTable.containsKey(sub)){
					subId = lookupTable.get(sub);
				}else {
					lookupTable.put(sub, SOIdent);
					subId = SOIdent;
					SOIdent++;
				}
				
				//Create look-up entry for Predicate. (This is going negative) 
				if (lookupTable.containsKey(prd)){
					prdId = lookupTable.get(prd);
				}else {
					lookupTable.put(prd, predIdent);
					prdId = predIdent;
					predIdent--;
				}
				
				//Create look-up entry for Object. 
				if (lookupTable.containsKey(obj)){
					objId = lookupTable.get(obj);
				}else {
					lookupTable.put(obj, SOIdent);
					objId = SOIdent;
					SOIdent++;
				}
				
				
				NodeStore.storeByNodeFwd(subId, prdId, objId); 
				tripleCnt++;
				NodeStore.storeByNodeFwd(objId, prdId, subId ); 
				tripleCnt++;
				
				
				if (DEBUG) {
					System.out.print("Triple ( "+ (tripleCnt) + " ): Subject: " +subject.toString());
					System.out.print("    Predicate: " + predicate.toString() + "    ");
					if (object instanceof Resource) {
						System.out.print(" Object: "+object.toString());
					} else {
						// object is a literal
						System.out.print(" Object: \"" + object.toString() + "\"");
					}

					System.out.println(" .");
					System.out.println("{ " + subId + ", " + prdId + ", " + objId + " }");
				} // end of if
				
			} // end of while 
		} // end of for j
	} // readRDF
	
//	

	/************************************************************
	 * @return the lookupTable
	 */
	public static HashBiMap<String, Integer> getLookupTable() {
		return lookupTable;
	}



	public void createData() {
		readRDF();

	}
	

	public static void main(String[] args) {
		 
		Store s = new Store(); 
		s. createData();
		GPSConvertor gps = new GPSConvertor(); 
		String gpsData = gps.convertToGps(NodeStore.nodeList); 
		GPSConvertor.writeGpsToFile(new File("C:/Users/Arsham_Mesbah/Desktop/test.txt"), gpsData); 
	 }

}
